package site.marqstree.latte.ec.main.personal.address;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import site.marqstree.latte.core.delegates.LatteDelegate;
import site.marqstree.latte.core.net.RestClient;
import site.marqstree.latte.core.net.callback.ISuccess;
import site.marqstree.latte.core.util.log.LatteLogger;
import site.marqstree.latte.ec.R;
import site.marqstree.latte.ec.R2;
import site.marqstree.latte.ec.profile.AppProfile;
import site.marqstree.latte.ui.recycler.MultipleItemEntity;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ec.main.personal.address
 * 文件名: AddressDelegate
 * 创建者: marqstree
 * 创建时间: 2020/2/5 19:55
 * 描述: TODO
 */
public class AddressDelegate extends LatteDelegate implements ISuccess {

    final AddressAdapter mAddressAdapter = new AddressAdapter();

    @BindView(R2.id.rv_address)
    RecyclerView mRecyclerView = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_address;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {

        final LinearLayoutManager manager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(mAddressAdapter);

        RestClient.builder()
                .url(AppProfile.ADDRESS_LIST_URL)
                .loader(getContext())
                .success(this)
                .build()
                .get();
    }

    @Override
    public void onSuccess(String response) {
        LatteLogger.d("AddressDelegate", response);

        final List<MultipleItemEntity> data =
                new AddressDataConverter().setJsonData(response).convert();

        mAddressAdapter.setNewData(data);

    }
}